using System;
using System.Data.Entity;
using System.Collections.Generic;
using Optum.SN.Infra.Core.Dependency.Service;

namespace Optum.SN.Infra.Core.TestBase
{
    /// <summary>
    /// Provides mock <see cref="DbContext"/> for unit tests.
    /// </summary>
    public class MockDbContext : DefaultDbContext
    {
        /// <summary>
        /// Creates default mock data map.
        /// </summary>
        /// <param name="registrations">Provides an array of entities and their mock data types.  
        /// At least one key value pair must be provided, otherwise an exception is raised.</param>
        protected MockDbContext(params KeyValuePair<Type, Type>[] registrations)
        {
            if (registrations == null || registrations.Length < 1)
                throw new Exception("At least one map item is required.");
            DefaultMockDataMap = new MockDataMap { RegistrationList = new List<KeyValuePair<Type, Type>>(registrations) };
        }
        /// <summary>
        /// Intercepts database reads and returns mock data.
        /// </summary>
        /// <typeparam name="T">The entity type requested from database.</typeparam>
        /// <returns>Mock data of specified entity type when available, otherwise throws exception.</returns>
        public override DbSet<T> Set<T>()
        {
            return DefaultMockDataMap.GetMockData<T>();
        }
        /// <summary>
        /// Gets/sets mock data map.
        /// </summary>
        private MockDataMap DefaultMockDataMap { get; set; }
    }
}